package com.qxsx.midi.activity.study;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.TextView;

import com.qxsx.midi.R;
import com.qxsx.midi.activity.base.BaseActivity;
import com.qxsx.midi.utils.MyNet;
import com.qxsx.midi.utils.callBackJsonArray;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

/**
 * Created by LYJ on 2015/9/16.
 * 考试指南
 */
public class AboutMiDiActivity extends BaseActivity implements OnClickListener {

    private TextView tv_bianji;
    private WebView wv_myWebView;
    private WebSettings mWebSettings;
    String title;
    String id;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_about_midi);
        Intent intent = this.getIntent();
        title = intent.getStringExtra("title");
        id = intent.getStringExtra("id");
        setTitle(title);
        setNavBtn(R.mipmap.back, 0);
        findById();
        inItData();
    }

    private void inItData() {
        JSONArray array = new JSONArray();
        JSONObject object = new JSONObject();
        try {
            object.put("article_id", id);
            array.put(object);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        MyNet.doPostJsonArray("8030", array, new callBackJsonArray() {
                    @Override
                    public void run(Object code, Object message, JSONArray array) {
                        // Log.e("-------------",code.toString());
                        if (code.toString().equals("200")) {

                            try {
                                JSONObject object = array.getJSONObject(0);
                                String url = object.getString("url");
                                inItWebView(url);
                            } catch (JSONException e) {
                                e.printStackTrace();
                            }


                        } else {
                            Log.e("code", code.toString());
                            Log.e("message", message.toString());

                        }
                    }
                }

        );

    }

    /**
     * 查找组件
     */
    private void findById() {


        wv_myWebView = (WebView) findViewById(R.id.aboutmidi_webview);


    }

    /**
     * 加载网页
     */
    private void inItWebView(String _url) {
//        wv_myWebView.requestFocus();
//        wv_myWebView.getSettings().setSupportZoom(true);
//        wv_myWebView.getSettings().setDefaultZoom(WebSettings.ZoomDensity.FAR);
//        wv_myWebView.getSettings().setBuiltInZoomControls(true);
//        wv_myWebView.getSettings().setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);
//        mWebSettings = wv_myWebView.getSettings();
//        mWebSettings.setJavaScriptEnabled(true);
//        mWebSettings.setUseWideViewPort(true);
//        mWebSettings.setPluginState(WebSettings.PluginState.ON);
//        wv_myWebView.setWebViewClient(new Callback());
//        mWebSettings.setLoadWithOverviewMode(true);
//        wv_myWebView.setHapticFeedbackEnabled(false);

        //支持javascript
        wv_myWebView.getSettings().setJavaScriptEnabled(true);
        // 设置可以支持缩放
        wv_myWebView.getSettings().setSupportZoom(true);
        // 设置出现缩放工具
        wv_myWebView.getSettings().setBuiltInZoomControls(true);
        //扩大比例的缩放
        wv_myWebView.getSettings().setUseWideViewPort(true);
        //自适应屏幕
        wv_myWebView.getSettings().setLayoutAlgorithm(WebSettings.LayoutAlgorithm.SINGLE_COLUMN);
        wv_myWebView.getSettings().setLoadWithOverviewMode(true);
        wv_myWebView.getSettings().setTextSize(WebSettings.TextSize.LARGEST);
        wv_myWebView.loadUrl(_url);


      //  wv_myWebView.loadUrl(_url);


    }

    @Override
    protected void HandleTitleBarEvent(TitleBar component, View v) {
        switch (component) {
            case LEFT:
                finish();
                break;
        }
    }

    private class Callback extends WebViewClient {
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            return (false);
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {

        }

    }


}
